﻿using System;
using System.Web;
using System.Web.Services;
using KZ.Exception;
using Process.BLL;

namespace BusinessService
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        public string WSLogin(string username, string password)
        {
            try
            {
                string sID = HttpContext.Current.Session.SessionID;
                if (ProviderBLL.ValidateLogin(sID, username, password))
                {
                    return sID;
                }
                throw new BusinessLogicException("Có lỗi khi đăng nhập hệ thống.");
            }
            catch
            {
                throw;
            }
        }

        [WebMethod(EnableSession = true)]
        public string AcceptTrans(string sID, string transID)
        {
            try
            {
                SessionBLL.ValidateSession(sID);

                TransactionBLL transBLL = new TransactionBLL();
                return transBLL.AcceptTrans(transID) ? "0" : "1";
            }
            catch
            {
                return "-2";
            }
        }

        [WebMethod(EnableSession = true)]
        public string RefuseTrans(string sID, string transID)
        {
            try
            {
                SessionBLL.ValidateSession(sID);

                TransactionBLL transBLL = new TransactionBLL();
                return transBLL.RefuseTrans(transID) ? "0" : "1";
            }
            catch
            {
                return "-2";
            }
        }

        [WebMethod(EnableSession = true)]
        public int CheckInventory(string sID, string transID)
        {
            try
            {
                SessionBLL.ValidateSession(sID);

                return InventoryBLL.CheckInventory(transID);
            }
            catch
            {
                return -1;
            }
        }

        [WebMethod(EnableSession = true)]
        public int UpdateInventory(string sID, string transID, int addedQuantity, decimal unitPrice, decimal transportPrice, decimal totalPrice)
        {
            try
            {
                SessionBLL.ValidateSession(sID);

                return InventoryBLL.UpdateInventory(transID, addedQuantity, unitPrice, transportPrice, totalPrice);
            }
            catch (BusinessLogicException)
            {
                return -1;
            }
            catch (Exception)
            {
                return -2;
            }
        }
    }
}